import axios from 'axios'

// 获取指定前缀的所有对象
export const listObjects = async (prefix = '') => {
  try {
    const response = await axios.get('/api/cos/list', {
      params: { prefix }
    })
    return response.data
  } catch (error) {
    console.error('获取对象列表失败:', error)
    throw error
  }
}

// 获取对象URL
export const getObjectUrl = async (key) => {
  try {
    const response = await axios.get(`/api/cos/url/${encodeURIComponent(key)}`)
    return response.data
  } catch (error) {
    console.error('获取对象URL失败:', error)
    throw error
  }
}

// 更新元数据
export const updateMetadata = async (data) => {
  try {
    const formData = new FormData()
    
    for (const key in data) {
      if (key === 'thumbnail' && data[key] instanceof File) {
        formData.append(key, data[key], 'thumbnail.png')
      } else if (data[key] !== undefined) {
        formData.append(key, data[key])
      }
    }
    
    const response = await axios.post('/api/metadata/update', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
    
    return response.data
  } catch (error) {
    console.error('更新元数据失败:', error)
    throw error
  }
}

// 获取元数据
export const getMetadata = async (cosKey) => {
  try {
    const response = await axios.get(`/api/metadata/${encodeURIComponent(cosKey)}`)
    return response.data
  } catch (error) {
    console.error('获取元数据失败:', error)
    throw error
  }
} 